package info.rongtong.action;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import info.rongtong.domain.Article;
import info.rongtong.domain.Picture;
import info.rongtong.domain.Review;
import info.rongtong.service.inter.ReviewServiceInter;

public class ReviewAction {

	private Review review;
	private Map<String, Object> map = new HashMap<String, Object>();
	private ReviewServiceInter reviewServiceImpl;
	private Integer pageSize=5;
	private Integer pageNow=1;
	private HttpServletRequest request = ServletActionContext.getRequest();
	/**
	 * 保存一个评论对象 
	 * @return
	 */
	public String saveReview() {
		review.setRtime(new Date());
		reviewServiceImpl.saveOrUpdate(review);
		// 写接口，写服务类
		map.put("status", "ok");
		return "json";
	}

	
	public String getReviewList(){
		String ip = request.getHeader("x-forwarded-for"); 
		 if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
		        ip = request.getHeader("Proxy-Client-IP");  
		    }  
		    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
		        ip = request.getHeader("WL-Proxy-Client-IP");  
		    }  
		    if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {  
		        ip = request.getRemoteAddr();  
		    }  
		System.out.println(ip);
		if(review.getAid()!=null){
			List list = reviewServiceImpl.getReviewListByCategory2(review.getAid(), Article.class, pageSize, pageNow);
//			for(int i=0;i<list.size();i++){
//				System.out.println(list.get(i));
//			}
			map.put("list",list );
			map.put("pageCount", reviewServiceImpl.getReviewListByCategoryCount(review.getAid(), Article.class, pageSize));
		}else if(review.getPid()!=null)
		{
			map.put("list", reviewServiceImpl.getReviewListByCategory2(review.getPid(), Picture.class, pageSize, pageNow));
			map.put("pageCount", reviewServiceImpl.getReviewListByCategoryCount(review.getPid(), Picture.class, pageSize));
		}
		return "json";
	}
	
	public String getReviewListByPage(){
		try{
		if(review.getAid()!=null){
			map.put("list", reviewServiceImpl.getReviewListByCategory2(review.getAid(), Article.class, pageSize, pageNow));
		}else if(review.getPid()!=null)
		{
			map.put("list", reviewServiceImpl.getReviewListByCategory2(review.getPid(), Picture.class, pageSize, pageNow));
		}
		}catch(Exception e){e.printStackTrace();}
		return "json";
	}
	
	// get与set方法区

	public Review getReview() {
		return review;
	}

	public void setReview(Review review) {
		this.review = review;
	}

	public Map<String, Object> getMap() {
		return map;
	}

	public ReviewServiceInter getReviewServiceImpl() {
		return reviewServiceImpl;
	}

	public void setReviewServiceImpl(ReviewServiceInter reviewServiceImpl) {
		this.reviewServiceImpl = reviewServiceImpl;
	}


	public Integer getPageSize() {
		return pageSize;
	}


	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}


	public Integer getPageNow() {
		return pageNow;
	}


	public void setPageNow(Integer pageNow) {
		this.pageNow = pageNow;
	}
	
	
}
